import os

# 设置代理
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

# 设置本地缓存目录
cache_dir = os.path.join('D:', os.path.sep, 'ModelSpace', 'Cache')
os.environ['HF_HOME'] = cache_dir

from transformers import pipeline

# 创建Pipeline任务
nlp = pipeline("question-answering", model="distilbert/distilbert-base-cased-distilled-squad")

# 执行问答任务
if __name__ == "__main__":
    # 上下文
    context = r"""
    Extractive Question Answering is the task of extracting an answer from a text given a question.
    An example of a question answering dataset is the SQuAD dataset, which is entirely based on that task.
    If you would like to fine-tune a model on a SQuAD task,
    you may leverage the examples/pytorch/question-answering/run_squad.py script.
    """

    # 执行任务
    result = nlp(context=context, question="What is a good example of a question answering dataset?")

    print(result)
    # 输出：{'score': 0.5152314901351929, 'start': 155, 'end': 168, 'answer': 'SQuAD dataset'}
